package com.example.word.controller;

import com.example.word.annotation.OptLog;
import com.example.word.demo.FileValidatorPool;
import com.example.word.dto.TempSelectDTO;
import com.example.word.dto.TemplateBackDTO;
import com.example.word.dto.TemplateDTO;
import com.example.word.enums.FilePathEnum;
import com.example.word.service.TemplateService;
import com.example.word.service.impl.TemplateServiceImpl;
import com.example.word.strategy.context.UploadStrategyContext;
import com.example.word.vo.ConditionVO;
import com.example.word.vo.PageResult;
import com.example.word.vo.Result;
import com.example.word.vo.TemplateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.example.word.constant.OptTypeConst.*;

/**
 * 模板控制器
 * @author yuwen
 * @date 2023/4/2
 */
@Api(tags = "模板模块")
@RestController
public class TemplateController {
//    @Autowired
//    private TemplateService templateService;

    @Autowired
    private UploadStrategyContext uploadStrategyContext;
    private final FileValidatorPool pool;
    private final TemplateService templateService;

    @Autowired
    public TemplateController(FileValidatorPool pool, TemplateService templateService) {
        this.pool = pool;
        this.templateService = templateService;
    }

    @ApiOperation(value = "教师上传模板存数据库")
    @PostMapping("/templates/upByTeacher")
    public Result<?> handleUpload(@RequestParam("template") MultipartFile file, String eName) throws Exception {
        TemplateServiceImpl templateServices = pool.borrowObject();
        String path = uploadStrategyContext.executeUploadStrategy(file, FilePathEnum.WD.getPath());
        try {
            if (this.templateService.validateFile(eName,path)) {
                // 文件验证通过
                return Result.ok();
            } else {
                // 文件格式不正确
                return Result.fail();
            }
        } finally {
            pool.returnObject(templateServices);
        }
    }


    /**
     * 查看后台模板
     *
     * @param condition 条件
     * @return {@link Result<TemplateBackDTO>} 后台文章列表
     */
    @ApiOperation(value = "查看后台文章")
    @GetMapping("/admin/templates")
    public Result<PageResult<TemplateBackDTO>> listTemplateBacks(ConditionVO condition) {
        return Result.ok(templateService.listTemplateBacks(condition));
    }
    /**
     * 删除模板
     *
     * @param templateIdList 模板id列表
     * @return {@link Result<>}
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "物理删除模板")
    @DeleteMapping("/admin/templates")
    public Result<?> deleteTemplates(@RequestBody List<Integer> templateIdList) {
        templateService.removeByIds(templateIdList);
        return Result.ok();
    }

    /**
     * 教师上传模板到数据库中
     *
     * @param
     * @return
     */
    //@OptLog(optType = SAVE)
//    @ApiOperation(value = "教师上传模板存数据库")
//    @PostMapping("/templates/upByTeacher")
//    public Result<?> fileUploads(@RequestParam("template") MultipartFile file, String eName) throws IOException {
//        templateService.uploadTemplate(eName, file);
//        return Result.ok();
//    }
    /**
     * 教师删除模板
     *
     * @param templateIdList 模板id列表
     * @return {@link Result<>}
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "物理删除模板")
    @DeleteMapping("/user/templates")
    public Result<?> deleteTemplatesByT(@RequestBody List<Integer> templateIdList) {
        templateService.removeByIds(templateIdList);
        return Result.ok();
    }

    /**
     * 教师添加或修改模板
     *
     * @param templateVO 模板
     * @return
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "教师添加或修改模板")
    @PostMapping("/user/templates")
    public Result<?> saveOrUpdateTemplate(@Valid @RequestBody TemplateVO templateVO) {
        System.out.println("templateVO = " + templateVO);
        templateService.saveOrUpdateTemplate(templateVO);
        return Result.ok();
    }




    /**
     * 查看教师自己模板
     *
     * @param condition 条件
     * @return {@link Result<TemplateBackDTO>} 后台文章列表
     */
    @ApiOperation(value = "查看后台文章")
    @GetMapping("/user/templates")
    public Result<PageResult<TemplateDTO>> listTemplatesById(ConditionVO condition) {
        return Result.ok(templateService.listTemplatesById(condition));
    }

//    /**
//     * 学生获取数据库模板
//     * @return
//     */
//    @ApiOperation(value = "学生获取数据库模板")
//    @GetMapping("/user/getTemplate")
//    public Result<PageResult<TemplateDTO>> getTeacherTemplate(){
//        return Result.ok(templateService.listTemplates());
//    }
    /**
     * 获取后台模板供学生选择
     *
     * @param condition 条件
     * @return {@link Result<TemplateBackDTO>} 后台文章列表
     */
    @ApiOperation(value = "查看后台文章")
    @GetMapping("/templateSelects")
    public Result<PageResult<TempSelectDTO>> listTemplateSelects(ConditionVO condition) {
        return Result.ok(templateService.listTemplateSelects(condition));
    }






}
